package com.poorstu.web;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

import com.common.base.web.BaseAction;
import com.poorstu.manager.FileManager;

public class FileUploadAction extends BaseAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		try{
		DynaActionForm upload=(DynaActionForm)form;
		String fileName=(String)upload.get("fileName");
		String fileContent=(String)upload.get("filecontent");
		FormFile file = (FormFile)upload.get("theFile");
		String fname=file.getFileName();
		int allUserId=(Integer)request.getSession().getAttribute("allUserId");
		
       
      //  String name = fileName.substring(fileName.lastIndexOf(".")); // 得到后缀名
		//String   fileType = fname.substring(fname.lastIndexOf(".")+1,fname.length());   

        String filePath = servlet.getServletContext().getRealPath("\\")+ "upload\\"+fname; // 上传文件存放的路径
      //  if (fileType.equals(".zip") || name.equals(".rar")) { // 判断后缀名是否为需求文件
            FileOutputStream fos = new FileOutputStream(filePath);
            if (fos!= null) { // 如果正确则不为空，不正确时为空
            	fos.write(file.getFileData());//开始写入
            	fos.flush();
            	fos.close();
            }
      //  }
        
        FileManager fileManager=(FileManager)getBean("fileManager");
        boolean b=fileManager.saveFile(fileName, fileContent, filePath,allUserId);
        if(b){
        	return mapping.findForward("succ");
        }
        
        return mapping.findForward("failure");
//		if(file != null){
//			System.out.println(file.getFileName());//打印出实际上传时所选文件的文件名
//			FileOutputStream fos = new FileOutputStream(filePath);//获取文件流对象
//			fos.write(file.getFileData());//开始写入
//			fos.flush();
//			fos.close();
//			}
		
		}catch(Exception e){
			e.printStackTrace();
			return mapping.findForward("failure");
		}
		
		

}
}